//
//  NSNumber+Ext.swift
//  StarTimesON
//
//  Created by Toj on 8/17/22.
//  Copyright © 2022 StarTimes. All rights reserved.
//

import Foundation

public extension Double {
    
    //精度减法运算
    func dd_subtracting(decimal value: Double) -> Double {
        let v1 = NSDecimalNumber(string: String(format: "%f", self))
        let v2 = NSDecimalNumber(string: String(format: "%f", value))
        return v1.subtracting(v2).doubleValue
    }
    
    //精度加法运算
    func dd_adding(decimal value: Double) -> Double {
        let v1 = NSDecimalNumber(string: String(format: "%f", self))
        let v2 = NSDecimalNumber(string: String(format: "%f", value))
        return v1.adding(v2).doubleValue
    }
}

// mn = money
public extension Double {
    
    var mn_format: String {
        return NSNumber(value: self).mn_decimal
    }
}

public extension NSNumber {
    
    var mn_decimal: String { 
        let format = NumberFormatter()
        format.numberStyle = .decimal
        format.decimalSeparator = "."
        format.groupingSeparator = ","
        format.usesGroupingSeparator = true
        format.groupingSize = 3
        return format.string(from: self) ?? ""
    }
}
